package org.liujing.jsw.client.border;

import org.liujing.jsw.client.ui.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.dom.client.*;
import com.google.gwt.core.client.GWT;

@SuppressWarnings("deprecation")
public class LineBorder extends AbstractBorder
{
    private static Border blackLine;
    private static Border grayLine;

    protected int thickness;
    protected String lineColor;
    protected boolean roundedCorners;
    protected Element element;
    protected boolean useWrapMode = false;

    /** Convenience method for getting the Color.black LineBorder of thickness 1.
      */
    public static Border createBlackLineBorder() {
        if (blackLine == null) {
            blackLine = new LineBorder("black", 1);
        }
        return blackLine;
    }

    /** Convenience method for getting the Color.gray LineBorder of thickness 1.
      */
    public static Border createGrayLineBorder() {
        if (grayLine == null) {
            grayLine = new LineBorder("#cccccc", 1);
        }
        return grayLine;
    }

    /** 
     * Creates a line border with the specified color and a 
     * thickness = 1.
     * @param color the color for the border
     */
    public LineBorder(String color) {
        this(color, 1, false);
    }

    /**
     * Creates a line border with the specified color and thickness.
     * @param color the color of the border
     * @param thickness the thickness of the border
     */
    public LineBorder(String color, int thickness)  {
        this(color, thickness, false);
    }

    private LineBorder(String color, int thickness, boolean roundedCorners)  {
        lineColor = color;
        this.thickness = thickness;
	this.roundedCorners = roundedCorners;
    }

     @Override
    public void createBorder(Component c, int x, int y, int width, int height) {
    	//LineBorderRenderer renderer = GWT.create(LineBorderRenderer.class);
    	//renderer.paint(c, x, y, width, height, thickness, lineColor);
    	
    	if(c.isDomElementSet()){
    		//useWrapMode = ( (c instanceof Container) || (w.getElement().getFirstChild() != null &&
    		//	w.getElement().getFirstChild().getNodeType() != Node.TEXT_NODE));
    		if(useWrapMode)
    		{
			element = Document.get().createDivElement();
			Style s = element.getStyle();
			s.setPosition(Style.Position.ABSOLUTE);
			s.setOverflow(Style.Overflow.HIDDEN);
			//s = w.getElement().getStyle();
			s.setBorderColor(lineColor);
			s.setBorderStyle(Style.BorderStyle.SOLID);
			s.setBorderWidth(thickness, Style.Unit.PX);
			s.setZIndex(-1);
			c.getDomElement().appendChild(element);
    		}else{
    			c.setChildElementOffset( 0-thickness, 0-thickness);
    			Style s = c.getDomElement().getStyle();
    			s.setBorderColor(lineColor);
			s.setBorderStyle(Style.BorderStyle.SOLID);
			s.setBorderWidth(thickness, Style.Unit.PX);
    		}
			//if(w.getElement().hasChildNodes()){
			//	Node childNode = w.getElement().getFirstChild();
			//	while(childNode != null){
			//		GWT.log("here childNode " + childNode);
			//		w.getElement().removeChild(childNode);
			//		element.appendChild(childNode);
			//		childNode = w.getElement().getFirstChild();
			//	}
			//}
    		//c.setContentElement(element);
    		//GWT.log("after border: " + w.getElement());
    	}
    }
    
    public void removeBorder(Component c){
    	
    	if(c.isDomElementSet()){
    		if(useWrapMode){
    			c.getDomElement().removeChild(element);
    		}else{				
				Style s = c.getDomElement().getStyle();
				s.clearBorderColor();
				s.clearBorderStyle();
				s.clearBorderWidth();
    		}
    	}
    }
    
    public void resize(Component c, Dimension size){
    	if(useWrapMode){
    		Style s = element.getStyle();
    		s.setWidth(size.width - (thickness << 1), Style.Unit.PX);
			s.setHeight(size.height - (thickness << 1), Style.Unit.PX);
		}else{
			size.width -= thickness << 1;
			size.height -= thickness << 1;
    	}
    }
    
    private static class LineBorderRenderer{
    	public LineBorderRenderer(){
    	}
    	
    	public void paint(JComponent c, int x, int y, int width, int height,
    		int thickness, String color){
    		
    	}
    }
    
    
    

    /**
     * Returns the insets of the border.
     * @param c the component for which this border insets value applies
     */
    public Insets getBorderInsets(Component c)       {
        return new Insets(thickness, thickness, thickness, thickness);
        //return new Insets(0, 0, 0, 0);
    }

    /** 
     * Reinitialize the insets parameter with this Border's current Insets. 
     * @param c the component for which this border insets value applies
     * @param insets the object to be reinitialized
     */
    public Insets getBorderInsets(Component c, Insets insets) {
        insets.left = insets.top = insets.right = insets.bottom = thickness;
        //insets.left = insets.top = insets.right = insets.bottom = 0;
        return insets;
    }

    /**
     * Returns the color of the border.
     */
    public String getLineColor()     {
        return lineColor;
    }

    /**
     * Returns the thickness of the border.
     */
    public int getThickness()       {
        return thickness;
    }

    /**
     * Returns whether this border will be drawn with rounded corners.
     * @since 1.3
     */
    public boolean getRoundedCorners() {
        return roundedCorners;
    }

    /**
     * Returns whether or not the border is opaque.
     */
    public boolean isBorderOpaque() { 
        return !roundedCorners; 
    }

}
